package textapp.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import textapp.business.RegisterService;
import textapp.connector.ConnectorMessage;
import textapp.connector.MessageCode;
import textapp.connector.ServerConnectorMessage;
import textapp.connector.message.RegResponse;
import textapp.dao.model.User;

/**
 * Servlet implementation class RegisterJsonServlet
 */
public class RegisterJsonServlet extends GenericJsonServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterJsonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	protected Object executeRequestJson(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Get parameters
		String identity = request.getParameter("iden");
		String name = request.getParameter("name");
		
		if (identity == null || name == null) {

			return new ServerConnectorMessage(MessageCode.FAIL);			
		}
		
		User u = new RegisterService().registerDevice(identity, name);
		if (u == null) {
			return new ServerConnectorMessage(MessageCode.FAIL);	
		}
		
		return new ServerConnectorMessage(MessageCode.SUCCESS, u);
		
	}

	

}
